package com.kindergarten.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kindergarten.mapper.UserChildrenMapper;
import com.kindergarten.pojo.Children;
import com.kindergarten.mapper.ChildrenMapper;
import com.kindergarten.pojo.UserChildren;
import com.kindergarten.service.IChildrenService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bruce
 * @since 2022-10-27
 */
@Service
public class ChildrenServiceImpl extends ServiceImpl<ChildrenMapper, Children> implements IChildrenService {

    @Resource
    ChildrenMapper childrenMapper;

    @Resource
    UserChildrenMapper userChildrenMapper;

    /**
     * 事务新增
     * 1. 新增Children表
     * 2. 新增 UserChildren
     * @param children
     * @param userChildren
     */
    @Override
    @Transactional(timeout = -1,readOnly = false,isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED)
    public void add(Children children, UserChildren userChildren) {
        childrenMapper.insert(children);
        //第一个表新增成功之后的ID值，第二张表要使用
        Integer childrenId = children.getId();
        userChildren.setChildrenId(childrenId);
        userChildrenMapper.insert(userChildren);
    }

    @Transactional(timeout = -1,readOnly = false,isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED)
    @Override
    public void update(Children children, UserChildren userChildren) {
        childrenMapper.updateById(children);
        userChildrenMapper.updateById(userChildren);
    }

    @Transactional(timeout = -1,readOnly = false,isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED)
    @Override
    public void delete(Integer id) {
        Children children=new Children();
        children.setId(id);
        children.setDel(1); //删除
        childrenMapper.updateById(children);

        UserChildren userChildren = userChildrenMapper.selectOne(new QueryWrapper<UserChildren>().eq("children_id", id));
        userChildren.setDel(1);//删除
        userChildrenMapper.updateById(userChildren);
    }
}
